<template>
  <div>
    <el-dialog v-model="dialogVisible" title="修改密码" width="500">
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="auto">
        <el-form-item label="输入旧密码：" prop="pwd">
          <el-input v-model="formData.pwd" placeholder="请输入旧密码" />
        </el-form-item>
        <el-form-item label="输入新密码：" prop="newPwd">
          <el-input v-model="formData.newPwd" placeholder="请输入新密码" />
        </el-form-item>
        <el-form-item label="再次输入新密码：" prop="newpass">
          <el-input v-model="formData.newpass" placeholder="请再次输入新密码" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { addcountgUpdate } from "@/api/system/AccountSet/AccoutSet";

const emit = defineEmits(["success"]);

const props = defineProps(["pwd"]);
const dialogVisible = defineModel<any>();
const formData = ref({
  pwd: "",
  newPwd: "",
  newpass: "",
});

const formRef = ref();

const validatepwd = (rule: any, value: string, callback: any) => {
  if (value !== props.pwd) {
    callback(new Error("请输入正确的旧密码"));
  } else {
    console.log(rule);
    callback();
  }
};

const validatenewpass = (rule: any, value: string, callback: any) => {
  if (value !== formData.value.newPwd) {
    callback(new Error("两次输入密码不一致"));
  } else {
    console.log(rule);

    callback();
  }
};

const rules = {
  pwd: [
    { required: true, message: "请输入旧密码", trigger: "blur" },
    { validator: validatepwd, trigger: "blur" },
  ],
  newPwd: [{ required: true, message: "请输入新密码", trigger: "blur" }],
  newpass: [
    { required: true, message: "请再次输入新密码", trigger: "blur" },
    { validator: validatenewpass, trigger: "blur" },
  ],
};

const submit = () => {
  formRef.value.validate((valid: any) => {
    if (valid) {
      addcountgUpdate(formData.value).then((res) => {
        ElMessage.success("修改成功");
        emit("success");
      });
    }
  });
};
</script>

<style></style>
